package com.xxxx.springbootsecurity02.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxxx.springbootsecurity02.exception.LoginException;
import com.xxxx.springbootsecurity02.mapper.SysMenuMapper;
import com.xxxx.springbootsecurity02.mapper.UserMapper;
import com.xxxx.springbootsecurity02.pojo.LoginUser;
import com.xxxx.springbootsecurity02.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println(username);
        SysUser sysUser = userMapper.selectOne(new QueryWrapper<SysUser>().eq("user_name",username));
        if (sysUser == null) {
            throw new LoginException("用户名错误-01");
        }
        //向UserDetails对象里面封装权限
        List<String> permissions = sysMenuMapper.findPerms(sysUser.getId());
        return new LoginUser(sysUser,permissions);
    }

}
